package com.nettytime.util.testjiebaonianbaos;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

public class ChildDelimiterChannelHandler extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        String delimiter = "_$";
        //对服务端返回的消息通过_$进行分隔，并且每次查找的最大大小为1024字节
        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, Unpooled.wrappedBuffer(delimiter.getBytes())));
        // 将前一步解码的数据转码为字符串
        ch.pipeline().addLast(new StringDecoder());
        // 对客户端发送的数据进行编码，这里主要是在客户端发送的数据最后添加分隔符
        ch.pipeline().addLast(new DelimiterBasedMessageEncoder(delimiter));
        // 最终的数据处理
        ch.pipeline().addLast(new NettyServerHandler());
    }
}
